package com.oa.ylms.apps.business.organization.impl;

import java.util.List;

import com.oa.ylms.apps.business.organization.IPostCategoryService;
import com.oa.ylms.apps.business.organization.IPostInfoService;
import com.oa.ylms.apps.business.organization.ISectionInfoService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.dao.organization.IPostInfoDao;
import com.oa.ylms.apps.entity.organization.PostInfoValue;

public class PostInfoServiceImpl implements IPostInfoService {
	private IPostCategoryService postCategoryService;
	private ISectionInfoService sectionInfoService;
	private PostInfoValue postInfo;
	private IPostInfoDao infoDao;

	@Override
	public int addPostInfo(int categoryId, int sectionId, String postName, String postNo)
			throws Exception {
		postInfo.setPostCategory(postCategoryService.queryByCategoryId(categoryId));
		if(postInfo.getPostCategory() != null){
			if(sectionId != 0)
				postInfo.setSectionInfo(sectionInfoService.queryBySectionId(sectionId));
			postInfo.setPostName(postName);
			postInfo.setPostNo(postNo);
			postInfo.setStates(IDEDStates.ENABLE);
			return infoDao.addPostInfo(postInfo);
		}
		return 0;
	}

	@Override
	public int countTPages(Integer categoryId, Integer sectionId, Integer states,
			int rows) throws Exception {
		if(categoryId != null)
		postInfo.setPostCategory(postCategoryService.queryByCategoryId(categoryId));
		if(sectionId != null)
		postInfo.setSectionInfo(sectionInfoService.queryBySectionId(sectionId));
		postInfo.setStates(states);
		return infoDao.countTPages(postInfo, rows);
	}
	
	@Override
	public int getTotalPages() throws Exception {
		return infoDao.getTotalPages();
	}

	@Override
	public boolean deletePostInfo(String postIds) throws Exception {
		String[] postIdStr = postIds.split(",");
		for(String postId : postIdStr){
			postInfo = queryByPostId(Integer.parseInt(postId));
			if(postInfo != null){
				postInfo.setStates(IDEDStates.DELETE);
				infoDao.editPostInfo(postInfo);
			}else{
				return false;
			}
		}
		return true;
	}

	@Override
	public boolean modifyPostInfo(int postId, int categoryId, int sectionId, String postName,String postNo) throws Exception {
		postInfo = queryByPostId(postId);
		postInfo.setPostCategory(postCategoryService.queryByCategoryId(categoryId));
		if(postInfo != null && postInfo.getPostCategory() != null){
			if(sectionId != 0)
				postInfo.setSectionInfo(sectionInfoService.queryBySectionId(sectionId));
			else
				postInfo.setSectionInfo(null);
			postInfo.setPostName(postName);
			postInfo.setPostNo(postNo);
			return infoDao.editPostInfo(postInfo);
		}
		return false;
	}

	@Override
	public PostInfoValue queryByPostId(int postId) throws Exception {
		postInfo.setPostId(postId);
		return infoDao.queryByPostId(postInfo);
	}
	
	@Override
	public PostInfoValue queryByCategoryIdAndSectionId(int categoryId,
			int sectionId) throws Exception {
		postInfo.setPostCategory(postCategoryService.queryByCategoryId(categoryId));
		postInfo.setSectionInfo(sectionInfoService.queryBySectionId(sectionId));
		if(postInfo.getPostCategory() != null && postInfo.getSectionInfo() != null){
			postInfo.setStates(IDEDStates.ENABLE);
			for(PostInfoValue piv : infoDao.queryPostInfoWithPage(postInfo, 1, 1)){
				return piv;
			}
		}
		return null;
	}

	@Override
	public List<PostInfoValue> queryPostInfoBySectionId(int sectionId)
			throws Exception {
		postInfo.setSectionInfo(sectionInfoService.queryBySectionId(sectionId));
		if(postInfo.getSectionInfo() != null){
			postInfo.setStates(IDEDStates.ENABLE);
			return infoDao.queryPostInfo(postInfo);
		}
		return null;
	}

	@Override
	public List<PostInfoValue> queryPostInfoWithPage(Integer categoryId,
			Integer sectionId, Integer states, int rows, int pageNo)
			throws Exception {
		if(categoryId != null)
		postInfo.setPostCategory(postCategoryService.queryByCategoryId(categoryId));
		if(sectionId != null)
		postInfo.setSectionInfo(sectionInfoService.queryBySectionId(sectionId));
		postInfo.setStates(states);
		return infoDao.queryPostInfoWithPage(postInfo, rows, pageNo);
	}

	@Override
	public boolean toDisable(int postId) throws Exception {
		postInfo = queryByPostId(postId);
		if(postInfo != null){
			postInfo.setStates(IDEDStates.DISABLE);
			return infoDao.editPostInfo(postInfo);
		}
		return false;
	}

	@Override
	public boolean toEnable(int postId) throws Exception {
		postInfo = queryByPostId(postId);
		if(postInfo != null){
			postInfo.setStates(IDEDStates.ENABLE);
			return infoDao.editPostInfo(postInfo);
		}
		return false;
	}
	
	
	public IPostCategoryService getPostCategoryService() {
		return postCategoryService;
	}

	public void setPostCategoryService(IPostCategoryService postCategoryService) {
		this.postCategoryService = postCategoryService;
	}

	public ISectionInfoService getSectionInfoService() {
		return sectionInfoService;
	}

	public void setSectionInfoService(ISectionInfoService sectionInfoService) {
		this.sectionInfoService = sectionInfoService;
	}

	public PostInfoValue getPostInfo() {
		return postInfo;
	}

	public void setPostInfo(PostInfoValue postInfo) {
		this.postInfo = postInfo;
	}

	public IPostInfoDao getInfoDao() {
		return infoDao;
	}

	public void setInfoDao(IPostInfoDao infoDao) {
		this.infoDao = infoDao;
	}

}
